﻿using Angle.Krill.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Angle.Krill
{
    public partial class Wallpaper : DummyWindow
    {
        public Wallpaper()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        const int PLAY_HOTKEY_ID = 10;
        private void Wallpaper_Load(object sender, EventArgs e)
        {
            const int MOD_NOREPEAT = 0x4000;
            const int MOD_CONTROL = 0x0002;
            const int MOD_ALT = 0x0001;
            NativeMethods.RegisterHotKey(this.Handle, PLAY_HOTKEY_ID, MOD_NOREPEAT | MOD_CONTROL | MOD_ALT, (int)'P');

            this.LoadWallpaper();
        }

        private void LoadWallpaper()
        {
            BeginInvoke(BeginInvoke);
        }

        private void BeginInvoke(Func<Delegate, IAsyncResult> beginInvoke)
        {
            var dpi = AntdUI.Config.Dpi;

            var panel_size = new Size((int)(258 * dpi), (int)(244 * dpi));
            int title_height = (int)(44 * dpi), size = (int)(10 * dpi), size2 = size * 2;

            flowPanel.PauseLayout = true;
            flowPanel.Controls.Clear();

            WallpaperHelper.Instance.RefreshWallpaperInfos();
            List<WallpaperInfo> wallpaperInfos = WallpaperHelper.Instance.WallpaperInfos;

            foreach (var item in wallpaperInfos)
            {
                var panel = new AntdUI.Panel
                {
                    BorderWidth = 1F,
                    Location = new Point(0, 0),
                    Margin = new Padding(0),
                    Shadow = 20,
                    Size = panel_size,
                    Tag = item
                };
                var pic = new PictureBox
                {
                    BackColor = Color.Transparent,
                    Dock = DockStyle.Fill,
                    Image = item.Image,
                    SizeMode = PictureBoxSizeMode.StretchImage,
                    Tag = item
                };

                var divider = new AntdUI.Divider
                {
                    BackColor = Color.Transparent,
                    Dock = DockStyle.Top,
                    Margin = new Padding(size),
                    Size = new Size(0, 1),
                    Tag = item
                };
                var label = new Label
                {
                    BackColor = Color.Transparent,
                    Dock = DockStyle.Top,
                    Font = new Font("Microsoft YaHei UI", 11F, FontStyle.Bold, GraphicsUnit.Point),
                    Padding = new Padding(size2, 0, 0, 0),
                    Size = new Size(0, title_height),
                    Text = item.Title,
                    TextAlign = ContentAlignment.MiddleLeft,
                    Tag = item
                };
                panel.Controls.Add(pic);
                panel.Controls.Add(divider);
                panel.Controls.Add(label);
                pic.Click += BtnClick;
                label.Click += BtnClick;
                flowPanel.Controls.Add(panel);
                panel.BringToFront();
            }
            flowPanel.PauseLayout = false;
        }

        private void BtnClick(object sender, EventArgs e)
        {
            if (sender is Control ctr)
            {
                if (ctr.Tag is WallpaperInfo wInfo)
                {
                    WallpaperHelper.Instance.LoadWallpaper(wInfo);
                }
            }
        }
    }
}
